package com.ihrm.system.controller;

import com.ihrm.common.controller.BaseController;
import com.ihrm.common.entity.Result;
import com.ihrm.common.entity.ResultCode;
import com.ihrm.common.exception.CommonException;
import com.ihrm.domain.system.Permission;
import com.ihrm.system.service.PermissionService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.List;
import java.util.Map;

/**
 * @author 小伟
 * @version V1.0
 * @Package com.ihrm.system.controller
 * @date 2020/12/14 13:33
 * @Copyright © 2020-2021 郑州恒星科技有限责任公司
 * 权限操作控制器
 */
@RestController
@RequestMapping("/sys")
public class PermissionController extends BaseController {
    @Autowired
    PermissionService permissionService;

    /**
     * 新增权限
     *
     * @param permissionMap 封装权限信息的map
     * @return 执行结果
     * @throws Exception 异常
     */
    @PostMapping("/permission")
    public Result savePermission(@RequestBody Map<String, Object> permissionMap) throws Exception {
        permissionMap.put("companyId", companyId);
        permissionService.savePermission(permissionMap);
        return new Result(ResultCode.SUCCESS);
    }

    /**
     * 根据id查询权限信息
     *
     * @param id 权限id
     * @return 查询结果
     * @throws Exception
     */
    @GetMapping("/permission/{id}")
    public Result findPermissionById(@PathVariable String id) throws Exception {
        Map<String, Object> map = permissionService.findById(id);
        return new Result(ResultCode.SUCCESS, map);
    }

    /**
     * 根据id修改权限信息
     *
     * @param id            权限id
     * @param permissionMap 封装权限信息的map
     * @return 修改结果
     * @throws Exception
     */
    @PutMapping("/permission/{id}")
    public Result updatePermission(@PathVariable String id, @RequestBody Map<String, Object> permissionMap) throws Exception {
        permissionMap.put("id", id);
        permissionService.updatePermission(permissionMap);
        return new Result(ResultCode.SUCCESS);
    }

    /**
     * 查询全部权限列表
     *
     * @param permissionMap 查询参数封装的map
     * @return 查询结果
     */
    @GetMapping("/permission")
    public Result findAll(@RequestParam Map<String, Object> permissionMap) {
        List<Permission> permissionList = permissionService.findAll(permissionMap);
        return new Result(ResultCode.SUCCESS, permissionList);
    }

    /**
     * 根据id删除权限
     * @param id 权限id
     * @return 删除结果
     * @throws CommonException
     */
    @DeleteMapping("/permission/{id}")
    public Result deletePermission(@PathVariable String id) throws CommonException {
        permissionService.deleteById(id);
        return new Result(ResultCode.SUCCESS);
    }
}
